package com.tbm.console.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 导入到EXCEL
* 类名称：ObjectExcelView.java
* 类描述： 
* @version 1.0
 */
public class ObjectExcelView extends AbstractXlsView{
	@Override
	protected void buildExcelDocument(Map<String, Object> map, Workbook workbook, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
		String excelName = map.get("name").toString() + ".xls";
		httpServletResponse.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(excelName,"utf-8"));
		httpServletResponse.setContentType("application/ms-excel; charset=UTF-8");
		httpServletResponse.setCharacterEncoding("UTF-8");
		List<String> titles = (List<String>) map.get("titles");
		List<List<String>> list = (List<List<String>>) map.get("varList");

		Sheet sheet = workbook.createSheet("User Detail");
		sheet.setDefaultColumnWidth(30);
		CellStyle style = workbook.createCellStyle();
		Font font = workbook.createFont();
		font.setFontName("Arial");
		style.setFillForegroundColor(HSSFColor.BLUE.index);
		style.setFillPattern((short) 1);
		font.setBold(true);
		font.setColor(HSSFColor.WHITE.index);
		style.setFont(font);
		Row header = sheet.createRow(0);
		for(int i=0;i<titles.size();i++){
			header.createCell(i).setCellValue(titles.get(i));
			header.getCell(i).setCellStyle(style);
		}
		int rowCount = 1;
		for (List<String> pg : list) {
			Row userRow = sheet.createRow(rowCount++);
			for(int i=0;i<pg.size();i++){
				userRow.createCell(i).setCellValue(pg.get(i));
			}
		}
	}
}
